<template>
	<view class="page">
		<!-- 顶部分类导航 -->
		<CategoryNav 
			v-model="currentCategory" 
			model="topic_catalog_list"
			@change="onCategoryChange" 
		/>

		<!-- 帖子列表 -->
		<scroll-view class="content" scroll-y="true" @scrolltolower="loadMore">
			<view class="waterfall-container">
				<view class="waterfall-column">
					<view class="post-item" v-for="(post, index) in leftColumnPosts" :key="`left-${index}`"
						@click="viewPost(post)">
						<!-- 大图帖子 -->
						<view v-if="post.type === 'image'" class="post-image-large">
							<image class="main-image" :src="post.image" mode="aspectFill" />
							<view class="post-content">
								<text class="post-title">{{ post.title }}</text>
								<view class="post-meta">
									<view class="author-info">
										<image class="avatar" :src="post.avatar" mode="aspectFill" />
										<text class="author-name">{{ post.name }}</text>
									</view>
									<view class="post-stats">
										<view class="stat-item">
											<image class="icon" src="/static/svg/heart.svg" mode="aspectFit" />
											<text class="stat-text">{{ post.likes }}</text>
										</view>
									</view>
								</view>
							</view>
						</view>

						<!-- 文字帖子 -->
						<view v-else-if="post.type === 'text'" class="post-text">
							<view class="post-header">
								<view class="author-info">
									<image class="avatar" :src="post.avatar" mode="aspectFill" />
									<view class="author-details">
										<text class="author-name">{{ post.name }}</text>
										<text class="post-time">{{ post.time }}</text>
									</view>
								</view>
								<image class="more-icon" src="/static/svg/more.svg" mode="aspectFit" />
							</view>
							<view class="post-body">
								<text class="post-title">{{ post.title }}</text>
								<text class="post-desc">{{ post.content }}</text>
							</view>
							<view class="post-footer">
								<view class="post-stats">
									<view class="stat-item">
										<image class="icon" src="/static/svg/heart.svg" mode="aspectFit" />
										<text class="stat-text">{{ post.likes }}</text>
									</view>
									<view class="stat-item">
										<image class="icon" src="/static/svg/comment.svg" mode="aspectFit" />
										<text class="stat-text">{{ post.comments }}</text>
									</view>
									<view class="stat-item">
										<image class="icon" src="/static/svg/share.svg" mode="aspectFit" />
									</view>
								</view>
							</view>
						</view>

						<!-- 卡片式帖子 -->
						<view v-else class="post-card">
							<view class="card-content">
								<view class="card-left">
									<text class="card-title">{{ post.title }}</text>
									<text class="card-desc">{{ post.content }}</text>
									<view class="card-meta">
										<text class="card-date">{{ post.date }}</text>
										<text class="card-category">{{ post.category }}</text>
									</view>
								</view>
								<view class="card-right" v-if="post.image">
									<image class="card-image" :src="post.image" mode="aspectFill" />
								</view>
							</view>
							<view class="card-footer">
								<view class="author-info">
									<image class="avatar small" :src="post.avatar" mode="aspectFill" />
									<text class="author-name">{{ post.name }}</text>
								</view>
								<view class="post-stats">
									<view class="stat-item">
										<image class="icon" src="/static/svg/heart.svg" mode="aspectFit" />
										<text class="stat-text">{{ post.likes }}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>

				<view class="waterfall-column">
					<view class="post-item" v-for="(post, index) in rightColumnPosts" :key="`right-${index}`"
						@click="viewPost(post)">
						<!-- 大图帖子 -->
						<view v-if="post.type === 'image'" class="post-image-large">
							<image class="main-image" :src="post.image" mode="aspectFill" />
							<view class="post-content">
								<text class="post-title">{{ post.title }}</text>
								<view class="post-meta">
									<view class="author-info">
										<image class="avatar" :src="post.avatar" mode="aspectFill" />
										<text class="author-name">{{ post.name }}</text>
									</view>
									<view class="post-stats">
										<view class="stat-item">
											<image class="icon" src="/static/svg/heart.svg" mode="aspectFit" />
											<text class="stat-text">{{ post.likes }}</text>
										</view>
									</view>
								</view>
							</view>
						</view>

						<!-- 文字帖子 -->
						<view v-else-if="post.type === 'text'" class="post-text">
							<view class="post-header">
								<view class="author-info">
									<image class="avatar" :src="post.avatar" mode="aspectFill" />
									<view class="author-details">
										<text class="author-name">{{ post.name }}</text>
										<text class="post-time">{{ post.time }}</text>
									</view>
								</view>
								<image class="more-icon" src="/static/svg/more.svg" mode="aspectFit" />
							</view>
							<view class="post-body">
								<text class="post-title">{{ post.title }}</text>
								<text class="post-desc">{{ post.content }}</text>
							</view>
							<view class="post-footer">
								<view class="post-stats">
									<view class="stat-item">
										<image class="icon" src="/static/svg/heart.svg" mode="aspectFit" />
										<text class="stat-text">{{ post.likes }}</text>
									</view>
									<view class="stat-item">
										<image class="icon" src="/static/svg/comment.svg" mode="aspectFit" />
										<text class="stat-text">{{ post.comments }}</text>
									</view>
									<view class="stat-item">
										<image class="icon" src="/static/svg/share.svg" mode="aspectFit" />
									</view>
								</view>
							</view>
						</view>

						<!-- 卡片式帖子 -->
						<view v-else class="post-card">
							<view class="card-content">
								<view class="card-left">
									<text class="card-title">{{ post.title }}</text>
									<text class="card-desc">{{ post.content }}</text>
									<view class="card-meta">
										<text class="card-date">{{ post.date }}</text>
										<text class="card-category">{{ post.category }}</text>
									</view>
								</view>
								<view class="card-right" v-if="post.image">
									<image class="card-image" :src="post.image" mode="aspectFill" />
								</view>
							</view>
							<view class="card-footer">
								<view class="author-info">
									<image class="avatar small" :src="post.avatar" mode="aspectFill" />
									<text class="author-name">{{ post.name }}</text>
								</view>
								<view class="post-stats">
									<view class="stat-item">
										<image class="icon" src="/static/svg/heart.svg" mode="aspectFit" />
										<text class="stat-text">{{ post.likes }}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 加载更多 -->
			<view class="load-more">
				<view class="loading-content" v-if="status === 'loading' || loading">
					<view class="loading-spinner"></view>
					<text class="load-text">加载中...</text>
				</view>
				<text class="load-text" v-else-if="status === 'noMore'">没有更多数据了</text>
				<text class="load-text" v-else-if="status === 'more'">上拉加载更多</text>
			</view>
		</scroll-view>

		<!-- 自定义底部导航 -->
		<bottom-nav />
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import BottomNav from '../home/components/BottomNav.vue'
import CategoryNav from '../../components/CategoryNav.vue'
import { usePageData } from "../../common/usePageData";

const searchObject = ref({
	catalog: null,
	page: 1,
	size: 10
})

const currentCategory = ref(0)

// 瀑布流数据
const leftColumnPosts = ref([])
const rightColumnPosts = ref([])


// 分配帖子到瀑布流列
const distributePostsToColumns = (list) => {
	if (list == null) {
		return
	}
	console.log("list", list)
	leftColumnPosts.value = []
	rightColumnPosts.value = []

	list.forEach((post, index) => {
		if (index % 2 === 0) {
			leftColumnPosts.value.push(post)
		} else {
			rightColumnPosts.value.push(post)
		}
	})
}
const { pageData, status, searchList, search, loading, loadMore } = usePageData("topic_list", searchObject, distributePostsToColumns)

// 分类切换事件处理
const onCategoryChange = (index, category) => {
	// 根据分类加载不同的数据
	searchObject.value.catalog = category?.id || null
	search()
}

// 查看帖子详情
const viewPost = (post) => {
	console.log('查看帖子:', post.title)
	// 这里可以跳转到帖子详情页
}

// loadMore函数已由usePageData提供，无需自定义

onMounted(() => {
	// 页面加载完成后的初始化操作
	distributePostsToColumns()
})
</script>

<style scoped>
.page {
	min-height: 100vh;
	background: #f8f9fa;
	padding-bottom: 90px;
}

/* 内容区域 */
.content {
	flex: 1;
	height: calc(100vh - 146px);
}

/* 瀑布流容器 */
.waterfall-container {
	display: flex;
	padding: 0 10px;
	gap: 10px;
}

.waterfall-column {
	flex: 1;
	display: flex;
	flex-direction: column;
}

.post-item {
	background: white;
	border-radius: 12px;
	margin: 5px 0;
	overflow: hidden;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
	break-inside: avoid;
}

/* 大图帖子样式 */
.post-image-large {
	position: relative;
}

.main-image {
	width: 100%;
	height: 200px;
}

.post-content {
	padding: 15px;
}

.post-title {
	font-size: 16px;
	font-weight: 500;
	color: #333;
	line-height: 1.4;
	margin-bottom: 10px;
}

.post-meta {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.author-info {
	display: flex;
	align-items: center;
}

.avatar {
	width: 32px;
	height: 32px;
	border-radius: 50%;
	margin-right: 8px;
}

.avatar.small {
	width: 24px;
	height: 24px;
}

.author-name {
	font-size: 14px;
	color: #666;
}

.author-details {
	display: flex;
	flex-direction: column;
}

.post-time {
	font-size: 12px;
	color: #999;
	margin-top: 2px;
}

.post-stats {
	display: flex;
	align-items: center;
}

.stat-item {
	display: flex;
	align-items: center;
	margin-left: 15px;
}

.icon {
	width: 16px;
	height: 16px;
	margin-right: 4px;
}

.stat-text {
	font-size: 12px;
	color: #666;
}

/* 文字帖子样式 */
.post-text {
	padding: 15px;
}

.post-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 12px;
}

.more-icon {
	width: 20px;
	height: 20px;
}

.post-body {
	margin-bottom: 15px;
}

.post-desc {
	font-size: 14px;
	color: #666;
	line-height: 1.5;
	margin-top: 8px;
}

.post-footer {
	border-top: 1px solid #f0f0f0;
	padding-top: 12px;
}

/* 卡片式帖子样式 */
.post-card {
	padding: 15px;
}

.card-content {
	display: flex;
	margin-bottom: 12px;
}

.card-left {
	flex: 1;
	margin-right: 12px;
}

.card-title {
	font-size: 16px;
	font-weight: 500;
	color: #333;
	margin-bottom: 8px;
}

.card-desc {
	font-size: 14px;
	color: #666;
	margin-bottom: 8px;
}

.card-meta {
	display: flex;
	gap: 10px;
}

.card-date,
.card-category {
	font-size: 12px;
	color: #999;
}

.card-right {
	width: 80px;
	height: 80px;
}

.card-image {
	width: 100%;
	height: 100%;
	border-radius: 8px;
}

.card-footer {
	display: flex;
	justify-content: space-between;
	align-items: center;
	border-top: 1px solid #f0f0f0;
	padding-top: 12px;
}

/* 加载更多 */
.load-more {
	text-align: center;
	padding: 20px;
	background: #f8f9fa;
}

.loading-content {
	display: flex;
	align-items: center;
	justify-content: center;
}

.loading-spinner {
	width: 16px;
	height: 16px;
	border: 2px solid #ddd;
	border-top: 2px solid #007AFF;
	border-radius: 50%;
	animation: spin 1s linear infinite;
	margin-right: 8px;
}

.load-text {
	font-size: 14px;
	color: #999;
}

@keyframes spin {
	0% { transform: rotate(0deg); }
	100% { transform: rotate(360deg); }
}
</style>
